package edu.scsu.csci.logging;

import org.apache.log4j.Logger;


/**
 * Default logging implementation for the framework.
 *
 * @author Maqbool Khan
 * @since jdk 1.5
 * @see
 */
public class DefaultLogger implements ILogger{

	/**
	 * log4j logger.
	 */
	private Logger log4j;
	
	/**
	 * current logLevel.
	 */
	private ELogLevel logLevel;
	
	/**
	 * Retruns an instance to the default logger.
	 */
	public DefaultLogger(){
		this.log4j = Logger.getLogger("WebApplicationFrameWorkLogger");
	}
		
	/* (non-Javadoc)
	 * @see edu.scsu.csci.logging.ILogger#log(edu.scsu.csci.logging.ELogLevel, java.lang.String[])
	 */
	public void log(ELogLevel logLevel, String... logMessage) {
		if(logMessage == null){
			return;
		}
		StringBuilder message = new StringBuilder(logMessage.length * 20);
		for(String s: logMessage){
			message.append(s).append(" ");
		}
		if(logLevel.equals(ELogLevel.DEGUG)){
			this.log4j.debug(message.toString());
		}
		else if(logLevel.equals(ELogLevel.DEGUG) || logLevel.equals(ELogLevel.INFO)
					&& !logLevel.equals(ELogLevel.OFF)){
			this.log4j.info(message.toString());
		}
		else if(logLevel.equals(ELogLevel.DEGUG) || logLevel.equals(ELogLevel.INFO)
					|| logLevel.equals(ELogLevel.WARNING) && !logLevel.equals(ELogLevel.OFF)){
			this.log4j.warn(message.toString());
		}
		else if(logLevel.equals(ELogLevel.DEGUG) || logLevel.equals(ELogLevel.INFO)
					|| logLevel.equals(ELogLevel.WARNING) || logLevel.equals(ELogLevel.ERROR)
					&& !logLevel.equals(ELogLevel.OFF)){
			this.log4j.error(message.toString());
		}
		else if(logLevel.equals(ELogLevel.DEGUG) || logLevel.equals(ELogLevel.INFO)
					|| logLevel.equals(ELogLevel.WARNING) || logLevel.equals(ELogLevel.ERROR)
					|| logLevel.equals(ELogLevel.FATAL) && !logLevel.equals(ELogLevel.OFF)){
			this.log4j.fatal(message.toString());
		}
		
	}

	/* (non-Javadoc)
	 * @see edu.scsu.csci.logging.ILogger#info(java.lang.String[])
	 */
	public void info(String... logMessage) {
		this.log(ELogLevel.INFO, logMessage);
	}

	/* (non-Javadoc)
	 * @see edu.scsu.csci.logging.ILogger#debug(java.lang.String[])
	 */
	public void debug(String... logMessage) {
		this.log(ELogLevel.DEGUG, logMessage);
	}

	/* (non-Javadoc)
	 * @see edu.scsu.csci.logging.ILogger#warn(java.lang.String[])
	 */
	public void warn(String... logMessage) {
		this.log(ELogLevel.WARNING, logMessage);
	}

	/* (non-Javadoc)
	 * @see edu.scsu.csci.logging.ILogger#error(java.lang.String[])
	 */
	public void error(String... logMessage) {
		this.log(ELogLevel.ERROR, logMessage);
	}

	/* (non-Javadoc)
	 * @see edu.scsu.csci.logging.ILogger#fatal(java.lang.String[])
	 */
	public void fatal(String... logMessage) {
		this.log(ELogLevel.FATAL, logMessage);
	}

	/* (non-Javadoc)
	 * @see edu.scsu.csci.logging.ILogger#setLogLevel(edu.scsu.csci.logging.ELogLevel)
	 */
	public void setLogLevel(ELogLevel logLevel) {
		this.logLevel = logLevel;
	}

	/* (non-Javadoc)
	 * @see edu.scsu.csci.logging.ILogger#getLogLevel()
	 */
	public ELogLevel getLogLevel() {
		return this.logLevel;
	}

	/* (non-Javadoc)
	 * @see edu.scsu.csci.logging.ILogger#error(java.lang.Throwable, java.lang.String[])
	 */
	public void error(Throwable e, String... logMessage) {
		if(logMessage == null) {
			return;
		}
		StringBuilder message = new StringBuilder(logMessage.length * 20);
		for(String s: logMessage){
			message.append(s).append(" ");
		}
		if(logLevel.equals(ELogLevel.DEGUG) || logLevel.equals(ELogLevel.INFO)
					|| logLevel.equals(ELogLevel.WARNING) || logLevel.equals(ELogLevel.ERROR)
					&& !logLevel.equals(ELogLevel.OFF)){
			this.log4j.error(message.toString(), e);
		}
	}

}
